<?php

namespace app\api\controller\v1\user\lottery;

use app\admin\model\DataStoreUser;
use app\admin\model\DataUserLotteryLog;
use app\api\controller\Api;

class Draw extends Api
{

    public function set(){
        $rule = [
            'store_id'    => 'require',
        ];
        $message  =   [
            'store_id.require' => '商家id不为空',
        ];

        $this->validate($this->post, $rule,$message);


        $store = new DataStoreUser();
        $store = $store ->with('lottery')->where(['id'=>$this->post['store_id']])->findOrEmpty();
        if($store->isEmpty())$this->error('商家不存在');

        $log = new DataUserLotteryLog();
        $log = $log->where(['store_id'=>$store->id,'batch'=>$store->batch])->count('id');
        if((intval($log) + 1) == intval($store->lottery['win'])){
            $win = 1;
        }else{
            $win = 0;
        }
        DataUserLotteryLog::create([
            'unid'=>$this->user['id'],
            'store_id'=>$store->id,
            'award'=>$store->lottery['award'],
            'win'=>$win,
        ]);
        $this->success('抽奖成功',['win'=>$win]);

    }
}